package ml.uuku.swipeheadertabs.ui.fragment.base;

import ml.uuku.swipeheadertabs.R;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import butterknife.Bind;

/**
 * 支持SwiperRefresh和RecyclerView的基类
 * 
 * @author <a href=http://my.oschina.net/uuku>uuku</a>
 * @version V0.1
 */
public abstract class BaseSwipeRefreshLayout<T extends RecyclerView.Adapter> extends BaseFragment {

    @Bind(R.id.view_swiperefresh)
    protected SwipeRefreshLayout mSwipeRefreshLayout;
    @Bind(R.id.view_recycler)
    protected RecyclerView mRecyclerView;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.base_swiperefresh_recyclerview;
    }

    @Override
    public void initView(View rootView) {
        setupSwipeRefreshLayout();
        setupRecyclerView();
    }

    private void setupSwipeRefreshLayout() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setColorSchemeResources(R.color.refresh_progress_1, R.color.refresh_progress_2,
                    R.color.refresh_progress_3);
            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    onRefreshData();
                }
            });
        }
    }

    private void setupRecyclerView() {
        if (mRecyclerView != null) {
            LinearLayoutManager mgr = new LinearLayoutManager(getActivity());
            mRecyclerView.setLayoutManager(mgr);
            mRecyclerView.setAdapter(getAdapter());
            mRecyclerView.addOnScrollListener(OnScrollToBottomListener(mgr));
        }
    }

    protected abstract T getAdapter();

    protected void onRefreshData() {
    }

    protected void onLoadDataMore() {
    }

    /**
     * 列表滑至底部的监听方法
     * <p>
     * 代码参考自:https://gist.github.com/ssinss/e06f12ef66c51252563e
     * </p>
     */
    private RecyclerView.OnScrollListener OnScrollToBottomListener(final LinearLayoutManager linearLayoutManager) {

        return new RecyclerView.OnScrollListener() {

            int firstVisibleItem, visibleItemCount, totalItemCount;
            int previousTotal = 0;
            int visibleThreshold = 5;
            private boolean loading = true;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                visibleItemCount = recyclerView.getChildCount();
                totalItemCount = linearLayoutManager.getItemCount();
                firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                    }
                }
                if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                    onLoadDataMore();
                    loading = true;
                }
            }
        };
    }
}
